package com.yxhl.zoume.core.car.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.car.model.SelectCityEntity;
import com.yxhl.zoume.core.car.model.SelectCityEvent;
import com.yxhl.zoume.data.rxbus.RxBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称：Ferrari
 * Created by menggod on 2016/9/2.
 */
public class SelectCityAdapter extends BaseAdapter<BaseAdapter.BaseViewHolder> {

    private static final String TAG = "SelectCityAdapter";
    private static final int LETTER_VIEW_TYPE = 1;
    private Context mContext;
    private List<SelectCityEntity> mCityEntityList;

    public SelectCityAdapter(Context context, List<SelectCityEntity> citylist) {
        mContext = context;
        mCityEntityList = citylist;
    }

    public void setCityEntityList(List<SelectCityEntity> list) {
        mCityEntityList = list;
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        switch (viewType) {
            case LETTER_VIEW_TYPE:
                View letterView = layoutInflater.inflate(R.layout.item_common_select_city_letter, parent, false);
                return new LetterViewHolder(letterView);
            default:
                View passengerView = layoutInflater.inflate(R.layout.item_common_select_city, parent, false);
                return new SelectCityHolder(passengerView);
        }
    }

    @Override
    public void onBindViewHolder(BaseAdapter.BaseViewHolder holder, int position) {

        if (holder instanceof LetterViewHolder) {
            LetterViewHolder viewHolder = (LetterViewHolder) holder;
            viewHolder.onBindViewHolder(position);
        } else if (holder instanceof SelectCityHolder) {
            SelectCityHolder viewHolder = (SelectCityHolder) holder;
            viewHolder.onBindViewHolder(position);

        }
    }

    @Override
    public int getItemCount() {
        return mCityEntityList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mCityEntityList != null) {
            SelectCityEntity cityEntity = mCityEntityList.get(position);
            String displayContent = cityEntity.getDisplayContent();
            if (!TextUtils.isEmpty(displayContent) && displayContent.matches("[A-Z]{1}")) {
                return LETTER_VIEW_TYPE;
            }
        }
        return super.getItemViewType(position);
    }


    class LetterViewHolder extends BaseAdapter.BaseViewHolder {

        @BindView(R.id.tv_item_select_city_letter)
        TextView letterTv;

        public LetterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void onBindViewHolder(int position) {
            if (position >= 0 && position < mCityEntityList.size()) {
                SelectCityEntity selectCityEntity = mCityEntityList.get(position);
                if (selectCityEntity != null) {
                    String letter = selectCityEntity.getDisplayContent();
                    if (!TextUtils.isEmpty(letter)) {
                        if (letter.matches("[A-Z]{1}")) {
                            setContent(letter);
                        }
                    }
                }
            }
        }

        void setContent(String str) {
            letterTv.setText(str);
        }
    }

    class SelectCityHolder extends BaseAdapter.BaseViewHolder {

        @BindView(R.id.tv_item_select_city)
        TextView mTvItemSelectCity;

        private SelectCityEntity mSelectCityEntity;

        public SelectCityHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.ll_item_select_city)
        public void onClick() {
            //     PromptUtils.showShort(mContext,mSelectCityEntity.getName()+"----"+mSelectCityEntity.getCenter());
            RxBus.getInstance().send(new SelectCityEvent(mSelectCityEntity.getName(), mSelectCityEntity.getCenter()));
        }

        public void onBindViewHolder(int position) {

            if (position >= 0 && position < mCityEntityList.size()) {
                mSelectCityEntity = mCityEntityList.get(position);
                if (mSelectCityEntity != null) {
                    String cityName = mSelectCityEntity.getName();
                    if (!TextUtils.isEmpty(cityName)) {
                        mTvItemSelectCity.setText(cityName);
                    }
                }
            }
        }
    }
}

